const { axios } = require('../../utils/http')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify.js'
const { formatTime } = require('../../utils/util')
Page({
  data: {
    loginStatus: false,
    active: 4,
    ifLogin: false,
    userInfo: {},
    activeName: '0',
    collapseConfig: [
      { title: '点赞', name: '1' },
      { title: '收藏', name: '2' },
      { title: '评论', name: '3' },
      { title: '留言', name: '4' }
    ],
    show: false,
    actions: [
      { name: '拍照', color: '#4186f1' },
      { name: '相册', color: '#4186f1' },
      { name: '预览', color: '#4186f1' }
    ],
    historyConfig: {
      goodHistory: [],
      remarkHistory: [],
      commentHistory: [],
      collectionHistory: []
    },
    openColl: '',
    config: {
      1: 'good',
      2: 'collect',
      3: 'remark',
      4: 'comment'
    }
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 4
      })
    }
    this.data.ifLogin = wx.getStorageSync('isLogin')
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      ifLogin: wx.getStorageSync('isLogin')
    })
    if (this.data.ifLogin) {
      this.getCollectionHistoryList()
      this.getGoodHistoryList()
      this.getOtherList()
    }
  },
  onPullDownRefresh() {
    // //调用刷新时将执行的方法
    // wx.showNavigationBarLoading()
    // this.getCollectionHistoryList()
    // this.getGoodHistoryList()
    // this.getOtherList()
    // setTimeout(() => {
    //   wx.hideNavigationBarLoading()
    //   wx.stopPullDownRefresh()
    // })
  },
  onChange(event) {
    this.setData({
      activeName: event.detail
    })
    const config = {
      1: 'good',
      2: 'collect',
      3: 'remark',
      4: 'comment'
    }
    this.setData({
      openColl: config[event.detail * 1]
    })
  },

  goLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  logout() {
    wx.clearStorage()
    this.onShow()
  },
  closeOverlay() {
    this.setData({
      show: false
    })
  },
  showSheet() {
    this.setData({
      show: true
    })
  },
  onSelect(event) {
    console.log(event.detail.name)
    const userInfo = wx.getStorageSync('userInfo')
    switch (event.detail.name) {
      case '拍照':
        this.getPictureFromAlbum(userInfo, 'camera')
        break
      case '相册':
        this.getPictureFromAlbum(userInfo, 'album')
        break
      case '预览':
        wx.previewImage({
          urls: [userInfo.avatar]
        })
        break
    }
  },
  getPictureFromAlbum(userInfo, type) {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: [type],
      success: res => {
        const token = wx.getStorageSync('token')
        const temPath = res.tempFiles[0].tempFilePath
        wx.compressImage({
          src: temPath,
          quality: 80,
          complete: path => {
            wx.uploadFile({
              url: 'https://www.ttzxh.icu/api/user/update',
              name: 'file',
              header: {
                'Content-Type': 'multipart/form-data',
                Authorization: token
              },
              filePath: path.tempFilePath,
              formData: {
                file: path.tempFilePath,
                id: userInfo.id + ''
              },
              success: res => {
                const { data } = JSON.parse(res.data)
                wx.setStorageSync('userInfo', {
                  ...userInfo,
                  avatar: data
                })
                this.setData({
                  userInfo: wx.getStorageSync('userInfo')
                })
                Notify({
                  type: 'success',
                  message: '头像上传成功'
                })
                this.setData({
                  show: false
                })
                this.onShow()
              }
            })
          }
        })
      }
    })
  },
  getGoodHistoryList() {
    // if (this.data.historyConfig.goodHistory.length !== 0) {
    //   return
    // }
    const { username } = this.data.userInfo
    axios
      .post('/history/search', {
        username
      })
      .then(res => {
        this.setData({
          historyConfig: { ...this.data.historyConfig, goodHistory: res.data.data }
        })
      })
  },
  getCollectionHistoryList() {
    // if (this.data.historyConfig.collectionHistory.length !== 0) {
    //   return
    // }
    const { username } = this.data.userInfo
    axios
      .post('/collect/search', {
        username
      })
      .then(res => {
        this.setData({
          historyConfig: { ...this.data.historyConfig, collectionHistory: res.data.data }
        })
      })
  },
  getOtherList() {
    const { username } = this.data.userInfo
    axios
      .post('/trace/trace', {
        username
      })
      .then(res => {
        this.setData({
          historyConfig: {
            ...this.data.historyConfig,
            remarkHistory: res.data.data[2],
            commentHistory: res.data.data[1]
          }
        })
      })
  },
  navAdvice() {
    wx.reLaunch({
      url: '/pages/comment/comment'
    })
  },
  nav(item) {
    console.log(item)
    const { blogId } = item.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/blogDetail/blogDetail?id=' + blogId
    })
  }
})
